<?php
namespace app\decorate\controller;
use app\AdminRest;
use think\App;
use think\facade\Db;
use think\Request;
use app\decorate\model\AdminSite as site_model;
use app\decorate\model\AdminProgress;
use app\decorate\model\AdminStyle;
use app\decorate\model\AdminArea;
use app\decorate\model\AdminHousewear;
use app\decorate\model\AdminStaffSite;

class AdminSite extends AdminRest{
    protected $model;
    protected $progress_model;
    protected $style_model;
    protected $area_model;
    protected $house_wear_model;
    protected $staff_site_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model           = new site_model();
        $this->progress_model  = new AdminProgress();
        $this->style_model     = new AdminStyle();
        $this->area_model      = new AdminArea();
        $this->house_wear_model= new AdminHousewear();
        $this->staff_site_model= new AdminStaffSite();

    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:工地列表
     */
    public function siteList(){
        $input = $this->_param;
        $where = "";
        if(!empty($input['title'])){
            $dis[] = ['title','like',"%\\".$input['title']."%"];
        }
        if(!empty($input['progress'])){
            $dis[] = ['progress','=',$input['progress']];
        }
        if(!empty($input['area'])){
            $dis[] = ['area','=',$input['area']];
        }
        if(!empty($input['designer'])){
            $designer = $input['designer'];
           $where = "find_in_set($designer,designer)";
        }
        $dis[] = ['uniacid','=',$this->_uniacid];
        $data  = $this->model->siteList($dis,$input['limit'],$where);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:58
     * @return \think\Response
     * descption:添加工地
     */
    public function siteAdd(){
        $input = $this->_input;
        $input = $this->model->changeData($input);
        $input['uniacid'] = $this->_uniacid;

        if (strlen($input['imgs']) > 6553) {
            return   $this->error('轮播图太多， 请减少轮播图数量');
        }

        Db::startTrans();
        $res   = $this->model->siteAdd($input);
        $this->staff_site_model->changeData($this->model->getLastInsID(),explode(',',$input['designer']), $this->_uniacid);
        Db::commit();
        return $this->success($res);
    }

    /**
     * User: chenniang
     * Date: 2019-10-09 15:59
     * @return \think\Response
     * descption:编辑|回显工地
     */
    public function siteUpdate(){
        $input = $this->_input;
        if(count($input)>1){
            Db::startTrans();
            $this->staff_site_model->changeData($input['id'],$input['designer'], $this->_uniacid);
            $input = $this->model->changeData($input);
            $data  = $this->model->siteUpdate(['id'=>$input['id']],$input);
            Db::commit();
        }else{
            $data = $this->model->siteInfo(['id'=>$input['id']]);

            $data['designer_info'] = $this->model->siteStaff($data['designer'],$this->_uniacid);

            $data['owner_text'] = Db::name('longbing_card_user')->where(['id'=>$data['owner']])->field('id,nickName')->find();
        }
        return $this->success($data);
    }



    /**
     * User: chenniang
     * Date: 2019-09-16 17:15
     * @return \think\Response
     * descption:上下架|删除
     */
    public function statusUpdate(){
        $input = $this->_input;
        $data  = $this->model->siteUpdate(['id'=>$input['id']],$input);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-16 17:15
     * @return \think\Response
     * descption:批量上下架|删除
     */
    public function statusUpdateAll(){
        $input = $this->_input;
        $dis[] = ['id','in',$input['id']];
        $data  = $this->model->siteUpdate($dis,['status'=>$input['status']]);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-12 15:55
     * @return void
     * descption:工地配置
     */
    public function siteSetting(){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $data['style']     = $this->style_model->styleSelect($dis);
        $data['area']      = $this->area_model->areaSelect($dis);
        $data['housewear'] = $this->house_wear_model->housewearSelect($dis);
        $data['progress']  = $this->progress_model->progressSelect($dis);
        $s_dis[] = ['uniacid','=',$this->_uniacid];
        $s_dis[] = ['status','=',1];
        $data['site'] = Db::name('longbing_card_decorate_site')->where($s_dis)->field('title,id')->select();
        $data['staff']= $this->selectStaff();
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-24 16:58
     * @return void
     * descption:选择员工
     */

    public function selectStaff(){
        $dis[] = ['a.uniacid','=',$this->_uniacid];
        $dis[] = ['a.is_staff','=',1];
        $data = Db::name('longbing_card_user_info')
            ->alias('a')
            ->leftJoin('longbing_card_job b','a.job_id = b.id')
            ->leftJoin('longbing_card_user c','c.id = a.fans_id')
            ->where($dis)
            ->field("a.fans_id, concat(IF(a.name = '',c.nickName,a.name),'-',ifnull(b.name,'暂无职位')) as name")
            ->group('a.fans_id')
            ->select()
            ->toArray();
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-10-12 16:31
     * @return \think\Response
     * descption:获取用户
     */
    public function getUser(){
        $input = $this->_param;
        $dis[] = ['uniacid','=',$this->_uniacid];
        if(!empty($input['name'])){
            $dis[] = ['nickName','like',"%\\".$input['name']."%"];
        }
        $data = Db::name('longbing_card_user')->where($dis)->field('id,nickName')->select();
        return $this->success($data);
    }


    public function diylist()
    {
        $param = $this->_param;
        $filter[] = ['uniacid', '=', $this->_uniacid];
        $filter[] = ['status' , '=', 1];
        if (!empty($param['keyword'])) {
            $filter[] = ['title', 'like', '%' . $param['keyword'] . '%' ];
        }
        $list = $this->model->where($filter)
            ->paginate(['list_rows' => $param['page_count'] ? $param['page_count'] : 10, 'page' => $param['page'] ? $param['page'] : 1])->toArray();

        return $this->success($list);
    }




}
